package com.mhb.generator.engine;

import com.mhb.generator.config.po.TableInfo;
import com.mhb.generator.config.rules.TemplateType;
import com.mhb.generator.util.StringUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.log4j.Log4j2;

import java.io.*;
import java.util.Map;

/**
 * @author 博博
 * @Title: FreemarkerTemplateEngine
 * @time 2020/1/3 0003 11:32
 */
@Log4j2
public class FreemarkerTemplateEngine extends TemplateAbstract {

    @Override
    public void writer(Map<String, Object> objectMap, String templatePath, String outputFile) throws Exception {
        // 判断视图类型
        if (templatePath.contains("view")) {
            TemplateType templateType = (TemplateType) objectMap.get("templateType");
            templatePath = getTemplateType(templateType, templatePath);
        } else {
            templatePath = StringUtil.format(templatePath, "ftl");
        }

        System.out.println(outputFile);
        create(objectMap,outputFile,templatePath);
    }

    private void create(Map<String, Object> objectMap,String createName, String fileName) {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        configuration.setDefaultEncoding("utf-8");
        configuration.setClassForTemplateLoading(this.getClass(),
                "/templates/");
        Template template = null;
        Writer out = null;
        try {
            template = configuration.getTemplate(fileName);
            template.setEncoding("utf-8");

            TableInfo tableInfo = (TableInfo) objectMap.get("table");

            File outFile = new File(createName);

            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(outFile), "utf-8"));

            template.process(objectMap, out);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("读取文件失败");
        } catch (TemplateException e) {
            e.printStackTrace();
            log.error("读取Template文件失败");
        }finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private String getTemplateType(TemplateType templateType, String templatePath) {
        if (templateType.equals(TemplateType.JSP)) {
            templatePath = StringUtil.format(templatePath, "jsp", "ftl");
        } else if (templateType.equals(TemplateType.FTL)) {
            templatePath = StringUtil.format(templatePath, "ftl", "ftl");
        } else if (templateType.equals(TemplateType.HTML)) {
            templatePath = StringUtil.format(templatePath, "html", "ftl");
        } else {
            templatePath = StringUtil.format(templatePath, "jsp", "ftl");
        }
        return templatePath;
    }

    @Override
    public String templateFilePath(String filePath) {
        return null;
    }
}
